package com.youlian.utils.http;

import java.io.InputStream;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.net.Proxy;
import android.text.TextUtils;
import android.util.Log;


/**
 *
 * @author simon
 *
 */
public class HTTPClientHelper {

	public static class PARAMS {

		public static final String CHARSET = HTTP.UTF_8;

		public static final int CONNECTION_TIMEOUT = 30 * 1000;

		public static final int SOCKET_TIMEOUT = 40 * 1000;

		public static final int SOCKET_BUFFER_SIZE = 20 * 1024;

		public static final boolean REDIRECT = true;

		public static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";

	}

	public static String getString(String url) throws Exception {
		String result = null;
		HttpGet request = new HttpGet(url);
		HttpResponse response = getHttpClient().execute(request);

		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			result = EntityUtils.toString(response.getEntity(), PARAMS.CHARSET);
		}
		// result = filterHtml(result);

		return result.trim();
	}

	public static InputStream getInputStream(String url) throws Exception {

		InputStream result = null;
		HttpGet request = new HttpGet(url);

		HttpResponse response = getHttpClient().execute(request);

		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			result = response.getEntity().getContent();
		}

		return result;
	}

	public static String filterHtml(String result) {
		if (null != result) {
			result = result.replace("&#039;", "'");
			result = result.replace("&amp;", "&");
			result = result.replace("&nbsp;", " ");
		}
		return result;
	}

	public static boolean isCMWAP() {
		String proxyHost = Proxy.getDefaultHost();
		return !TextUtils.isEmpty(proxyHost);// ��ֵ����Wap
	}

	private static HttpClient getHttpClient() {
		BasicHttpParams httpParams = new BasicHttpParams();

		if (isCMWAP()) {
			HttpHost proxy = new HttpHost("10.0.0.172", 80);
			httpParams.setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
		}
		HttpConnectionParams.setConnectionTimeout(httpParams,
				PARAMS.CONNECTION_TIMEOUT);
		HttpConnectionParams.setSoTimeout(httpParams, PARAMS.SOCKET_TIMEOUT);
		HttpConnectionParams.setSocketBufferSize(httpParams,
				PARAMS.SOCKET_BUFFER_SIZE);

		HttpClientParams.setRedirecting(httpParams, PARAMS.REDIRECT);

		HttpProtocolParams.setUserAgent(httpParams, PARAMS.USER_AGENT);
		return new DefaultHttpClient(httpParams);
	}
}
